'''
dict = {}
#key : value
#method1
student1 = {"name":"kyy","age":18,"city":"Jinan"}
print(student1)
#method2
# student2 = dict(name="kyy",age=18,city="jinan")
# print(student2)

# print(student1["name"])
# print(student1["gender"]) # 报错

# print(student1.get("name"))
print(student1.get("gender","unknown"))

for key,value in student1.items():
    print(f"{key}----------{value}")

for key in student1.keys():
    print(key)

for value in student1.values():
    print(value)

print(len(student1)) #3

if "name" in student1:
    print("exist")

# 修改和添加
student1["name"] = "Marvin"
student1["gender"] = "male"

# 删除
del student1["gender"]
'''
def insertScoreByName(name,score,subject):
    # print(student[name])
    # print(student[name][subject])
    if name in students_dict:
        students_dict[name][subject]= score
    else:
        # 错误 ，name 不存在
        print(f"错误，{name} 不存在")
        students_dict[name] = {subject:score}


# 计算某个学生的平均成绩
def calculate_average(name):
    """
    计算指定学生的平均成绩
    如果学生不存在或没有成绩，返回None
    """
    if name not in students_dict:
        print(f"错误：学生{name}不存在")
        return None
    grades = students_dict[name].values()
    if not grades:  # 检查是否有成绩
        print(f"{name}没有成绩记录")
        return None
    sum = 0
    for grade in grades:
        sum+=grade
    # average = sum(grades) / len(grades)
    average = sum / len(grades)
    return round(average, 2)  # 保留两位小数

#输出所有学生的姓名和总分
def totalScore():
    for name, courses in students_dict.items():
        total = sum(courses.values())
        print(f"{name}: 总分 {total}")


students_dict={
    "kyy":{"math":90,"CS":82},
    "Mar":{"math":80,"CS":92},
    "wax":{"math":30,"CS":22}
}
insertScoreByName("Teth",100,"math")
print(students_dict)
print(calculate_average("kyy"))
totalScore()